package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.vo.CustomerGoodsVo;
import com.atguigu.jxc.entity.vo.SaleDataByDayOrMonthVo;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.service.SaleListService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * @author qingyu
 * @create 2022-01-02 18:30
 * @description
 */
@RestController
@RequestMapping("/saleListGoods")
public class SaleListGoodsController {
    @Autowired
    private SaleListGoodsService saleListGoodsService;
    @Autowired
    private SaleListService saleListService;


    /**
     * 销售单保存 并减库存
     * @param saleList
     * @param saleListGoodsStr
     * @param saleNumber
     * @return
     */
    @PostMapping("/save")
    public String save(SaleList saleList, String saleListGoodsStr, @RequestParam("saleNumber") String saleNumber){
        saleListGoodsService.save(saleList,saleListGoodsStr,saleNumber);
        Gson gson = new Gson();
        return gson.toJson(new ServiceVO<>(100,"请求成功",null));
    }

    /**
     * 销售单查询（可查询条件：销售单号、客户、付款状态）
     * @param saleNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("/list")
    public String list(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
        List<SaleList> saleListList = saleListService.list(saleNumber,customerId,state,sTime,eTime);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",saleListList);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    /**
     * 查询销售单商品信息
     * @param saleListId
     * @return
     */
    @PostMapping("/goodsList")
    public String goodsList(Integer saleListId){
        List<SaleListGoods> saleListGoodsList = saleListGoodsService.goodsList(saleListId);
        Map<String,Object> map = new Hashtable<>();
        map.put("rows",saleListGoodsList);
        Gson gson = new Gson();
        return gson.toJson(map);
    }
    /**
     * 通过saleListId删除销售单商品信息
     * @param saleListId
     * @return
     */
    @PostMapping("/delete")
    public String delete(Integer saleListId){
        saleListGoodsService.delete(saleListId);
        Gson gson = new Gson();
        return gson.toJson(new ServiceVO<SaleListGoods>(100,"请求成功",null));
    }




    /**
     * 支付结算（修改销售单付款状态）
     * @param saleListId 销售清单id
     * @return
     */
    @PostMapping("/updateState")
    public ServiceVO updateState(@RequestParam("saleListId") Integer saleListId){
        saleListGoodsService.updateState(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    /**
     * 销售统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime 起始时间
     * @param eTime 结束时间
     * @param goodsTypeId 商品类型id
     * @param codeOrName 商品编码或名称
     * @return
     */
    @PostMapping("/count")
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName){

        List<CustomerGoodsVo> goodsVos = saleListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);

        Gson gson = new Gson();
        return gson.toJson(goodsVos);
    }


    /**
     * 按日统计接口
     * @param sTime 起始时间
     * @param eTime 结束时间
     * @return
     */
    @PostMapping("/getSaleDataByDay")
    public String getSaleDataByDay(String sTime, String eTime){

        List<SaleDataByDayOrMonthVo> byDayVos = saleListGoodsService.getSaleDataByDay(sTime,eTime);

        Gson gson = new Gson();
        return gson.toJson(byDayVos);
    }

    /**
     * 按月统计接口
     * @param sTime 起始时间
     * @param eTime 结束时间
     * @return
     */
    @PostMapping("/getSaleDataByMonth")
    public String getSaleDataByMonth(String sTime, String eTime){

        List<SaleDataByDayOrMonthVo> byDayVos = saleListGoodsService.getSaleDataByMonth(sTime,eTime);

        Gson gson = new Gson();
        return gson.toJson(byDayVos);
    }




}
